/*
 * hkh-project
 * leafteamwork.blogspot.com
 * Tool: Netbeans 6.7.1 - SQL Server 2005
 */
package vn.com.hkh.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import vn.com.hkh.dto.BookDTO;
import vn.com.hkh.dto.ContactDTO;
import vn.com.hkh.dto.PageDTO;
import vn.com.hkh.dto.RoomDTO;
import vn.com.hkh.service.BookService;
import vn.com.hkh.service.ContactService;
import vn.com.hkh.service.PageService;
import vn.com.hkh.service.RoomService;
import vn.com.hkh.util.DatabaseUtil;

/**
 *
 * @author leaf
 */
public class HomeAction extends HttpServlet {

    private PageService ps = null;
    private RoomService rs = null;
    private ContactService cs = null;
    private BookService bs = null;

    @Override
    public void init() throws ServletException {
        super.init();
        ps = new PageService();
        rs = new RoomService();
        cs = new ContactService();
        bs = new BookService();
    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        String _forward = "";
        PrintWriter out = response.getWriter();
        try {
            // T00. Set language for display
            String _lang = request.getParameter("lang");
            if (_lang == null) {
                if (request.getSession().getAttribute("lang") == null) {
                    _lang = "vi";
                } else {
                    _lang = (String) request.getSession().getAttribute("lang");
                }
            } else if (_lang.equalsIgnoreCase("en")) {
                _lang = "en";
            } else {
                _lang = "vi";
            }
            request.getSession().setAttribute("lang", _lang);
            String _alias = (String) request.getSession().getAttribute("lang");
            // T01. CLIENT PROCESS
            String _action = request.getParameter("act");
            _action = (_action == null) ? "home" : _action;

            if (_action.equalsIgnoreCase("page")) {
                String _id = request.getParameter("id");
                PageDTO _page = ps.findByPrimaryKey(_id);
                request.setAttribute("page", _page);
                _forward = "/page.jsp";
            } else if (_action.equalsIgnoreCase("room")) {
                String _id = request.getParameter("id");
                RoomDTO _room = rs.findByPrimaryKey(_id);
                request.setAttribute("room", _room);
                _forward = "/room.jsp";
            } else if (_action.equalsIgnoreCase("home")) {
                Collection<RoomDTO> _rooms = rs.findByAlias(_alias + "display%");
                request.setAttribute("rooms", _rooms);
                _forward = "/home.jsp";
            }

            if (_action.equalsIgnoreCase("addcontact")) {
                //add contact
                String _message = "";
                ContactDTO _contactDTO = new ContactDTO();
                _contactDTO.setId(DatabaseUtil.generateUUID());
                _contactDTO.setFullname(request.getParameter("fullname"));
                _contactDTO.setEmail(request.getParameter("email"));
                _contactDTO.setAddress(request.getParameter("address"));
                _contactDTO.setPhone(request.getParameter("phone"));
                _contactDTO.setTitle(request.getParameter("title"));
                _contactDTO.setContent(request.getParameter("content"));
                //_contactDTO.setWhen(request.getParameter("when"));

                ContactDTO _result = cs.create(_contactDTO);
                if (_result == null) {
                    if (request.getSession().getAttribute("lang").equals("en")) {
                        _message = "Error!";
                    } else {
                        _message = "Lỗi!";
                    }
                    request.setAttribute("contact", _contactDTO);                  

                } else {
                    if (request.getSession().getAttribute("lang").equals("en")) {
                        _message = "Successful!";
                    } else {
                        _message = "Thành công!";
                    }                   
                }               
                request.setAttribute("message", _message);
            }

            //add reservation vao csdl
            if (_action.equalsIgnoreCase("reservation")) {
                String _customerName = request.getParameter("customer");
                String _email = request.getParameter("email");
                String _phone = request.getParameter("phone");
                //String _address;
                //arrive date
                String _arriveDay = request.getParameter("arriveDay");
                String _arriveMonth = request.getParameter("arriveMonth");
                String _arriveYear = request.getParameter("arriveYear");
                String _arriveDate = _arriveYear + "-" + _arriveMonth + "-" + _arriveDay;
                //Departure date
                String _departDay = request.getParameter("departDay");
                String _departMonth = request.getParameter("departMonth");
                String _departYear = request.getParameter("departYear");
                String _departDate = _departYear + "-" + _departMonth + "-" + _departDay;

                String _roomCount = request.getParameter("rooms");
                String _adults = request.getParameter("adults");
                String _children = request.getParameter("children");
                String _otherRequest = request.getParameter("content");

                BookDTO _bookDTO = new BookDTO();
                _bookDTO.setId(DatabaseUtil.generateUUID());
                _bookDTO.setCustomerFullname(_customerName);
                _bookDTO.setCustomerEmail(_email);
                _bookDTO.setCustomerPhone(_phone);
                _bookDTO.setArriveDate(_arriveDate);
                _bookDTO.setDepartureDate(_departDate);
                _bookDTO.setRoomCount(Integer.parseInt(_roomCount));
                _bookDTO.setAdultsCount(Integer.parseInt(_adults));
                _bookDTO.setChildrenCount(Integer.parseInt(_children));
                _bookDTO.setOtherRequest(_otherRequest);

                BookDTO _result = bs.create(_bookDTO);
                String _message ="";
                if (_result == null) {
                    if (request.getSession().getAttribute("lang").equals("en")) {
                        _message = "Error!";
                    } else {
                        _message = "Lỗi!";
                    }
                    request.setAttribute("book", _bookDTO);
                   

                } else {
                    if (request.getSession().getAttribute("lang").equals("en")) {
                        _message = "Successful!";
                    } else {
                        _message = "Thành công!";
                    }
                   
                }
                request.setAttribute("message", _message);                
            }



            Collection<PageDTO> _leftpages = ps.findByAlias(_alias + "left%");
            Collection<PageDTO> _toppages = ps.findByAlias(_alias + "top%");
            Collection<PageDTO> _bottompages = ps.findByAlias(_alias + "bottom%");
            Collection<PageDTO> _otherpages = ps.findByAlias(_alias + "other%");
            request.setAttribute("leftpages", _leftpages);
            request.setAttribute("toppages", _toppages);
            request.setAttribute("bottompages", _bottompages);
            request.setAttribute("otherpages", _otherpages);
            request.setAttribute("forward", _forward);

            RequestDispatcher rd = request.getRequestDispatcher("homelayout.jsp");
            rd.forward(request, response);


        } finally {
            out.close();
        }
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    @Override
    public void destroy() {
        super.destroy();
        ps = null;
        rs = null;
        cs = null;
        bs = null;
    }
}
